package com.brucekong.slm.controller.content;

import com.brucekong.slm.common.PageResult;
import com.brucekong.slm.model.content.ArticleDomain;
import com.brucekong.slm.model.content.CommentDomain;
import com.brucekong.slm.service.content.ArticleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 内容控制类
 */
@RestController
@RequestMapping("/content")
@Slf4j
@Api(value = "内容模块", tags = "内容模块")
public class ArticleController {

    private PageResult pageResult = new PageResult();

    @Autowired
    private ArticleService articleService;

    /**
     * 文章列表
     *
     * @return pageResult 获取文章列表
     */
    @GetMapping("/article")
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "文章列表", notes = "获取文章列表", code = 200, produces = "application/json", responseContainer = "List", response = PageResult.class)
    public PageResult getArticleList(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                     @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        log.info("/content/article");

        PageInfo<ArticleDomain> articleServiceArticleList = articleService.getArticleList(page, limit);

        pageResult.setCode(0);
        pageResult.setMsg(null);
        pageResult.setSuccess(true);
        pageResult.setCount(articleServiceArticleList.getTotal());
        pageResult.setData(articleServiceArticleList.getList());
        return pageResult;
    }

    /**
     * 标签
     *
     * @return pageResult 获取标签列表
     */
    @GetMapping("/tags")
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "标签列表", notes = "获取标签列表", code = 200, produces = "application/json", responseContainer = "List", response = PageResult.class)
    public PageResult getTagsList(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                  @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        log.info("/content/tags");

        PageInfo<Map> tags = articleService.getTagsList(page, limit);

        pageResult.setCode(0);
        pageResult.setMsg(null);
        pageResult.setSuccess(true);
        pageResult.setCount(tags.getTotal());
        pageResult.setData(tags.getList());
        return pageResult;
    }

    /**
     * 评论
     *
     * @return pageResult 获取评论列表
     */
    @GetMapping("/comments")
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "评论列表", notes = "获取评论列表", code = 200, produces = "application/json", responseContainer = "List", response = PageResult.class)
    public PageResult getCommentsList(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                      @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        log.info("/content/comments");

        PageInfo<CommentDomain> commentsList = articleService.getCommentsList(page, limit);

        pageResult.setCode(0);
        pageResult.setMsg(null);
        pageResult.setSuccess(true);
        pageResult.setCount(commentsList.getTotal());
        pageResult.setData(commentsList.getList());
        return pageResult;
    }

}